<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属部门" prop="deptguid">
        <el-select v-model="queryParams.deptguid" placeholder="请选择所属部门" filterable clearable size="small">
          <el-option
            v-for="item in departments"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="种植户" prop="userid">
        <el-select v-model="queryParams.userid" placeholder="请选择种植户" filterable clearable size="small">
          <el-option
            v-for="item in farmers"
            :key="item.userid"
            :label="item.name"
            :value="item.userid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['farmer:farmerinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['farmer:farmerinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['farmer:farmerinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['farmer:farmerinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="farmerinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属部门" align="center" prop="depname" width="140" />
      <el-table-column label="种植户" align="center" prop="name" />
      <el-table-column label="地址" align="center" prop="address" width="230" />
      <el-table-column label="电话" align="center" prop="telephone" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['farmer:farmerinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['farmer:farmerinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改种植户档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="种植户" prop="userid">
          <el-select v-model="form.userid" filterable placeholder="请选择种植户">
            <el-option
              v-for="item in farmersADD"
              :key="item.guid"
              :label="item.realname"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门" prop="deptguid">
          <el-select v-model="form.deptguid" placeholder="请选择所属部门" filterable clearable size="small">
            <el-option
              v-for="item in departmentsADD"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="province">
          <v-distpicker  :placeholders="placeholders"></v-distpicker>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="银行账户" prop="accountname">
          <el-input v-model="form.accountname" placeholder="请输入银行账户名" />
        </el-form-item>
        <el-form-item label="开户行" prop="bankname">
          <el-input v-model="form.bankname" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="accountno">
          <el-input v-model="form.accountno" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="有无仓库" prop="selfstore">
          <el-radio-group v-model="form.selfstore" size="small">
            <el-radio v-for="(item, index) in selfstores" :key="index" :label="item.value"
                      :disabled="item.disabled">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="容量" prop="storecapcity" v-if="form.selfstore == 1">
          <el-input v-model="form.storecapcity" placeholder="请输入自有仓库存储容量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFarmerinfo, getFarmerinfo, getHasPermissionFarmers, updateFarmerinfo, addFarmerinfo, delFarmerinfo} from "@/api/farmer/farmerinfo";
import {tuserList, findDepartment} from "@/api/public";
import {listDept} from "@/api/system/employee";
import {getPermisForDepart} from "@/api/organization/department";

export default {
  name: "Farmerinfo",
  components: {
  },
  data() {
    return {
      placeholders: {
        province: '---- 省 ----',
        city: '--- 市 ---',
        area: '--- 区 ---',
      },
      selfstores: [{
        "label": "有",
        "value": 1
      }, {
        "label": "无",
        "value": 2
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 种植户档案表格数据
      farmerinfoList: [],
      departments: [],
      departmentsADD: [],
      farmers: [],
      farmersADD: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptguid: null,
        name: null,
        status: null,
        userid: null,
        functionTypes: 24,
        departype: 3,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getDeparts();
    this.getFarmers();
    this.getFarmersADD();
    this.getDepartsADD();
    this.getDicts("famerbatch_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询种植户档案列表 */
    getList() {
      this.loading = true;
      listFarmerinfo(this.queryParams).then(response => {
        this.farmerinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDeparts() {
      getPermisForDepart(this.queryParams).then(response => {
        this.departments = response.data;
      });
    },
    getDepartsADD() {
      findDepartment({permission: 41}).then(response => {
        this.departmentsADD = response.data;
      });
    },
    getFarmers(){
      getHasPermissionFarmers().then(response => {
        this.farmers = response.data;
      });
    },
    getFarmersADD(){
      tuserList({usertype: 1}).then(response => {
        this.farmersADD = response.data;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        userid: null,
        deptguid: null,
        depname: null,
        name: null,
        province: null,
        city: null,
        district: null,
        address: null,
        odistrict: null,
        telephone: null,
        accountname: null,
        bankname: null,
        accountno: null,
        remark: null,
        lastorderdate: null,
        status: null,
        selfstore: null,
        storecapcity: null,
        operatorid: null,
        createtime: null,
        lastupdtime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加种植户档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids
      getFarmerinfo(guid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改种植户档案";
        this.placeholders.province = response.data.province;
        this.placeholders.city = response.data.city;
        this.placeholders.area = response.data.district;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != null) {
            updateFarmerinfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFarmerinfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFarmerinfo(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有种植户档案数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFarmerinfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
